package edu.uwlax.cs.oayonlinestore.client.gui.salesdept;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.HSpacer;
import edu.uwlax.cs.oayonlinestore.client.utils.SelectionList;
import edu.uwlax.cs.oayonlinestore.client.utils.Table;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.client.utils.Table.AddRow;
import edu.uwlax.cs.oayonlinestore.vo.InventoryReportVO;
import edu.uwlax.cs.oayonlinestore.vo.SnapshotVO;

public class PageSalInventoryReport extends TitlePage {

	private class CreateSnHandler extends DefaultAsyncCallback implements
			ClickListener {

		public void onClick(Widget arg0) {
			OnlinestoreRService.Util.getInstance().createSnapshot(this);
		}

		public void onSuccess(Object arg0) {
			reloadSnapshots();
			Window.alert("Snapshot successfully created.");
		}
	}

	private class LoadInvRepHandler extends DefaultAsyncCallback implements
			ClickListener {

		public void onClick(Widget arg0) {
			snapshot1.setEnabled(false);
			snapshot2.setEnabled(false);
			int s1 = Integer.parseInt(snapshot1.getSelection());
			int s2 = Integer.parseInt(snapshot2.getSelection());
			OnlinestoreRService.Util.getInstance().getInventoryReport(s1, s2,
					this);
		}

		public void onFailure(Throwable arg0) {
			snapshot1.setEnabled(true);
			snapshot2.setEnabled(true);
			super.onFailure(arg0);
		}

		public void onSuccess(Object arg0) {
			InventoryReportVO[] rep = (InventoryReportVO[]) arg0;
			table.cleanExceptHeader();
			for (int i = 0; i < rep.length; i++)
				table.addItem(rep[i]);
			snapshot1.setEnabled(true);
			snapshot2.setEnabled(true);
		}
	}

	private class SnapshotLoadHandler extends DefaultAsyncCallback {
		public void onSuccess(Object arg0) {
			SnapshotVO[] sn = (SnapshotVO[]) arg0;
			snapshot1.clear();
			snapshot2.clear();
			for (int i = 0; i < sn.length; i++) {
				snapshot1.addItem(sn[i].getDate() + "", sn[i].getId() + "");
				snapshot2.addItem(sn[i].getDate() + "", sn[i].getId() + "");
			}
		}
	}

	private class StockReportTable extends DekoTable {

		public StockReportTable() {
			super();
			addTitle("Brand");
			addTitle("Product");
			addTitle("Opening");
			addTitle("Sold");
			addTitle("Returned");
			addTitle("Added");
			addTitle("Closing");
		}

		public void addItem(InventoryReportVO i) {
			AddRow r = addRow();
			r.add(i.getProduct().getBrand());
			r.add(i.getProduct().getName());
			r.add(i.getOpening());
			r.add(i.getSold());
			r.add(i.getReturned());
			r.add(i.getAdded());
			r.add(i.getClosing());
		}

	}

	private SelectionList snapshot1;

	private SelectionList snapshot2;

	private StockReportTable table;

	protected void create() {
		setTitle("Inventory Report");
		add(createDateSel());
		add(new HSpacer());
		add(table = new StockReportTable());
	}

	private Widget createDateSel() {
		Table t = new Table();
		t.setCellPadding(4);
		AddRow r = t.addRow();
		r.add("Show report between snapshots:");
		r.add(new Button("Create Snapshot Now", new CreateSnHandler()))
				.setColspan(2).setHAlign(r.RIGHT);
		r = t.addRow();
		r.add(snapshot1 = new SelectionList());
		r.add(snapshot2 = new SelectionList());
		r.add(new Button("Show", new LoadInvRepHandler()));
		reloadSnapshots();
		return t;
	}

	public String getID() {
		return "InventoryReport";
	}

	private void reloadSnapshots() {
		OnlinestoreRService.Util.getInstance().getSnapshotList(
				new SnapshotLoadHandler());
	}

}
